package view.nn.com.myapplication;

import android.app.Activity;
import android.os.Bundle;
import android.view.View;
import android.view.ViewGroup;
import android.webkit.WebSettings;
import android.webkit.WebView;
import android.webkit.WebViewClient;
import android.widget.ProgressBar;

import com.google.android.gms.ads.AdListener;
import com.google.android.gms.ads.AdRequest;
import com.google.android.gms.ads.AdSize;
import com.google.android.gms.ads.AdView;
import com.google.android.gms.ads.InterstitialAd;

/**
 */
public class BaseActivity extends Activity {

    protected boolean isShow = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
    }

    InterstitialAd interstitialAd;

    public void showCP() {

        interstitialAd = new InterstitialAd(this);
        interstitialAd.setAdUnitId(getString(R.string.cp));
        interstitialAd.setAdListener(new AdListener() {
            @Override
            public void onAdLoaded() {
                interstitialAd.show();
            }

            @Override
            public void onAdFailedToLoad(int errorCode) {
            }

            @Override
            public void onAdClosed() {
            }
        });

        AdRequest adRequest = new AdRequest.Builder()
                .setRequestAgent("aaa:bbb").build();
        interstitialAd.loadAd(adRequest);
    }

    public void showHP(ViewGroup parent) {
        String adUnitId = getString(R.string.hp);
        AdView adView = new AdView(BaseActivity.this);
        adView.setAdUnitId(adUnitId);
        adView.setAdSize(AdSize.BANNER);
        AdRequest request = new AdRequest.Builder().build();
        adView.loadAd(request);
        if (parent != null && adUnitId != null && !adUnitId.equals("")) {
            parent.addView(adView);
        }
    }

    public void setWebView(WebView webView, String url, final ProgressBar progress) {
        WebSettings webSettings = webView.getSettings();
        webSettings.setJavaScriptEnabled(true);
        webView.setWebViewClient(new WebViewClient() {
            @Override
            public boolean shouldOverrideUrlLoading(WebView view, String url) {
                // TODO Auto-generated method stub
                //返回值是true的时候控制去WebView打开，为false调用系统浏览器或第三方浏览器
                view.loadUrl(url);
                return true;
            }

            @Override
            public void onPageFinished(WebView view, String url) {
                // TODO Auto-generated method stub
                super.onPageFinished(view, url);
                progress.setVisibility(View.GONE);
            }


        });
        webView.loadUrl(url);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        if (isShow)
            showCP();
    }
}
